//
//  HELMeSettingVC.m
//  HandheldELearn
//
//  Created by wsj2015 on 2017/1/7.
//  Copyright © 2017年 陈武琦. All rights reserved.
//

#import "HELMeSettingVC.h"
#import "HELNormalCell.h"
#import "NCMotifypasswordVC.h"
#import "JCAlertView.h"
#import "BTAdviceVC.h"
#import "CWQ_aboutUsVC.h"
#import "HELPDFfileCacheTool.h"
#import "BFPaperButton.h"
#import "HELBindPhoneVC.h"
#import "HELNavigationController.h"
#import "HELFeedbackListVC.h"
@interface HELMeSettingVC ()<UIAlertViewDelegate>

@end

@implementation HELMeSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)configUI{
  
    self.dataArray = [@[@[@"客服电话"],@[@"修改密码",@"绑定手机",@"清除缓存",@"意见反馈",@"关于我们"]] copy];
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELNormalCell" bundle:nil] forCellReuseIdentifier:@"HELNormalCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.title = @"个人设置";
    
    CGFloat f = kScreenFactor;
    BFPaperButton *btn = [[BFPaperButton alloc] initWithFrame:CGRectMake(30, kScreenHeight -100 - 64, kScreenWidth -60, 49) raised:YES];
    [btn setTitle:@"退出登录" forState:UIControlStateNormal];
    
    btn.titleLabel.font = [UIFont systemFontOfSize:18];
    [btn setBackgroundColor:kMainColor];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
   

    [self.view addSubview:btn];
    
//    BFPaperButton *bfRaisedSmartSmall = [[BFPaperButton alloc] initWithFrame:CGRectMake(20, 375, 135, 83) raised:YES];
//    [bfRaisedSmartSmall setBackgroundColor:[UIColor colorWithRed:33.f/255.f green:150.f/255.f blue:243.f/255.f alpha:1]];
//    bfRaisedSmartSmall.titleLabel.numberOfLines = 0;
//    bfRaisedSmartSmall.titleLabel.font = [UIFont systemFontOfSize:10.f];
//    [bfRaisedSmartSmall setTitle:@"BFPaperButton Raised: Smart Color" forState:UIControlStateNormal];
//    [bfRaisedSmartSmall setTitleFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:15.f]];
//    [bfRaisedSmartSmall addTarget:self action:@selector(buttonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:bfRaisedSmartSmall];
    
    
}

- (void)logout {
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否退出？" message:nil delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
    alert.tag = 102;
    [alert show];
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
      
        [self.navigationController popViewControllerAnimated:YES];
        cwqHudSuccess(@"成功退出登录~");
        [[NSNotificationCenter defaultCenter] postNotificationName:LogoutNotification object:nil];
        
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HELNormalCell *cell =[tableView dequeueReusableCellWithIdentifier:@"HELNormalCell"];
    
    UIView *view = [UIView new];
    view.alpha = .7;
    view.frame = cell.bounds;
    view.backgroundColor = kMainColor;
    cell.selectedBackgroundView =view;
    NSString *title =self.dataArray[indexPath.section][indexPath.row];
    cell.mainTitleL.text = title;
    
    
    if (indexPath.section==1) {
        if (indexPath.row ==2) {
            cell.rightTitleL.text = @"清除缓存";
            cell.rightTitleL.hidden = NO;
            [HELPDFfileCacheTool sizeForAllCourseWithCallback:^(CGFloat size) {
                cell.rightTitleL.text = [NSString stringWithFormat:@"%0.1fM",size];
            }];
        }else if (indexPath.row == 1){
            cell.rightTitleL.hidden  =NO;
            cell.rightTitleL.text = [[HELAppManager userInfo].phoneBindStatus integerValue]==0?@"未绑定":@"已绑定";
        }else{
            cell.rightTitleL.hidden = YES;
        }
   
    }else{
        cell.rightTitleL.hidden = YES;
    }
    
    return cell;
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section==0) {
        if (indexPath.row == 0) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://02162471677"]];
        }
    }else{
        switch (indexPath.row) {
            case 0:{
                NCMotifypasswordVC *vc =[NCMotifypasswordVC new];
              
                [self.navigationController pushViewController:vc animated:YES];
                break;
            }
              
            case 1:{
                HELNavigationController *nv =[[HELNavigationController alloc] initWithRootViewController:[HELBindPhoneVC new]];
                
                [self presentViewController:nv animated:YES completion:nil];
              
                    
        
                
                break;
            }
            case 2:{
                [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"是否删除所有课程缓存" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"否" Click:nil ButtonType:JCAlertViewButtonTypeWarn ButtonTitle:@"确认" Click:^{
                    [HELPDFfileCacheTool removeAllCourses:^{
                        [[RLMCourseManager manager] deleteAllObjects];
                        [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];
                        cwqHudSuccess(@"删除缓存成功~");
                    }];
                }];

              
                break;
            }
            case 3:{
//                BTAdviceVC *vc =[BTAdviceVC new];
//                [self.navigationController pushViewController:vc animated:YES];
           
                HELFeedbackListVC *vc =[HELFeedbackListVC new];
                [self.navigationController pushViewController:vc animated:YES];

                break;
            }
            case 4:{
          
                CWQ_aboutUsVC *vc =[CWQ_aboutUsVC new];
                [self.navigationController pushViewController:vc animated:YES];
                
                break;
            }
            
                
            default:
                break;
        }
    
    }
    
    
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view =[UIView new];
    view.frame =CGRectMake(0, 0, kScreenWidth, 10);
    view.backgroundColor =[UIColor clearColor];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

@end
